package edu.the.last.graphic;

import java.text.DecimalFormat;
import java.util.Date;

import javax.swing.JLabel;

import edu.the.last.Messages;

/**
 * this class is responsible for all the times visible below the wave form
 * 
 * @author albertcsani
 * 
 */
public class TimeCounter extends Thread {
	private JLabel infoLabel;
	private boolean count;
	private String info, postInfo;
	private double limit = Double.MAX_VALUE;

	/**
	 * constructor
	 * 
	 * @param infoLabel
	 *            the label where the time info whill be displayed
	 */
	public TimeCounter(JLabel infoLabel) {
		this.infoLabel = infoLabel;
	}

	/**
	 * @param info
	 *            a string that will appear before the time
	 * @param postInfo
	 *            a string that will appear after the time
	 */
	public void setInfo(String info, String postInfo) {
		this.info = info;
		this.postInfo = postInfo;
	}

	/**
	 * sets the time limits
	 * 
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(double limit) {
		this.limit = limit;
	}
/**
 * starts the counting
 */
	public void stopCount() {
		count = false;
	}
/**
 * counts, and displays it
 */
	public void run() {
		count = true;
		long startMsec = new Date().getTime();
		long curMsec = new Date().getTime();
		double time = curMsec - startMsec;
		DecimalFormat df = new DecimalFormat(Messages.getString("FelLe.Format")); //$NON-NLS-1$
		while (count && time < limit) {
			curMsec = new Date().getTime();
			time = curMsec - startMsec;
			time = time / 1000.0;
			infoLabel.setText(info + df.format(time) + postInfo);
			Thread.currentThread();
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		if (limit < Double.MAX_VALUE) {
			infoLabel.setText(info + df.format(limit) + postInfo);
		}
	}

}
